#include"touch.h"
#include"lcd.h"
#include"bmp.h"
lv_obj_t * btn1;
lv_obj_t * btn2;
static int img_index = 0;
static LCD lcd;
static char *bmp[7] = {
	"image/1.bmp",
	"image/2.bmp",
	"image/3.bmp",
	"image/4.bmp",
	"image/5.bmp",
	"image/6.bmp",
	"image/7.bmp",
};
// 触摸屏初始化
void touchscreen_init(TouchScreen *ts)
{
	ts->fd = open("/dev/input/event0", O_RDWR);
	if(ts->fd < 0)
	{
		perror("open touchscreen fail");
		return ;
	}
	
	// 获取X， Y的坐标最大值
	if (ioctl(ts->fd, EVIOCGABS(ABS_X), &ts->abs_x) == -1) {//打开文件描述符对应文件，获取绝对X轴的请求，存入abs结构体里面
        perror("获取X轴参数失败");
        close(ts->fd);
        return ;
    }
    if (ioctl(ts->fd, EVIOCGABS(ABS_Y), &ts->abs_y) == -1) {
        perror("获取Y轴参数失败");
        close(ts->fd);
        return ;
    } 
}

void touchscreen_uninit(TouchScreen *ts)
{
	close(ts->fd);
}


// 按下响应(获取x和y的值，立即返回)
void ts_press(Coordinate *coor, TouchScreen *ts)
{
	// 定义输入事件结构体
    struct input_event buf;
	coor->x = -1, coor->y = -1;
    while(1)
    {
        // bzero(&buf, sizeof(buf));
        read(ts->fd, &buf, sizeof(buf));

		// 绝对事件（触摸屏）
        if(buf.type == EV_ABS && buf.code == ABS_X)
			coor->x = buf.value * 800 / ts->abs_x.maximum;

        if(buf.type == EV_ABS && buf.code == ABS_Y)
			coor->y = buf.value * 480 / ts->abs_y.maximum;
		
		if(coor->x != -1 && coor->y != -1)
			break;
    }
}

// 松开响应(离开屏幕，返回)
void ts_loosen(Coordinate *coor, TouchScreen *ts)
{
	// 定义输入事件结构体
    struct input_event buf;
	// coor->x = -1, coor->y = -1;//加上这行触摸屏会卡顿（快点快松），
    while(1)
    {
        // bzero(&buf, sizeof(buf));
        read(ts->fd, &buf, sizeof(buf));

		// 按键事件
        if(buf.type == EV_KEY && buf.code == BTN_TOUCH)
		{
			if(buf.value == 0 && coor->x != -1 && coor->y != -1)
				break;
		}

		// 绝对事件（触摸屏）
        if(buf.type == EV_ABS && buf.code == ABS_X)
			coor->x = buf.value * 800 / ts->abs_x.maximum;

        if(buf.type == EV_ABS && buf.code == ABS_Y)
			coor->y = buf.value * 480 / ts->abs_y.maximum;
    }
}

//设置按钮
static void event_handler(lv_event_t * e)
{
    lv_obj_t *target = lv_event_get_target(e);
	if (target == btn1)
	{
		img_index = ((img_index - 1) == -1) ? 6 : img_index - 1;
	}
	else if (target == btn2)
	{
		img_index = ((img_index + 1) == 7) ? 0 : img_index + 1;
	}
	anybmp(0, 0, bmp[img_index], &lcd);
}


static void lv_example_btn(void)
{
    lv_obj_t * label1;

    btn1 = lv_btn_create(lv_scr_act());
	lv_obj_set_size(btn1,150,100);
	lv_obj_set_style_border_width(btn1,0,LV_STATE_DEFAULT);
	lv_obj_set_style_border_opa(btn1,LV_OPA_TRANSP,LV_STATE_DEFAULT);
    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btn1, LV_ALIGN_BOTTOM_RIGHT, 0, -150);

    label1 = lv_label_create(btn1);
    lv_label_set_text(label1, LV_SYMBOL_NEXT);
	lv_obj_set_size(label1,75,50);
    lv_obj_center(label1);


	lv_obj_t * label2;
	

    btn2 = lv_btn_create(lv_scr_act());
	lv_obj_set_size(btn2,150,100);
	lv_obj_set_style_border_width(btn2,0,LV_STATE_DEFAULT);
	lv_obj_set_style_border_opa(btn2,LV_OPA_TRANSP,LV_STATE_DEFAULT);
    lv_obj_add_event_cb(btn2, event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btn2, LV_ALIGN_BOTTOM_LEFT, 0, -150);

    label2 = lv_label_create(btn2);
    lv_label_set_text(label2, LV_SYMBOL_PREV);
	lv_obj_set_size(label2,75,50);
    lv_obj_center(label2);
}

// 相册
void myphoto_example(void)
{
	anybmp(0, 0, bmp[img_index], &lcd);
	lv_example_btn();
	lcd_init(&lcd);
	
	lv_timer_handler();
	
	// while (1)
	// {
	// 	lv_timer_handler(); 
	// 	anybmp(0, 0, bmp[img_index], &lcd);
	// }

	while (1)
    {
        lv_timer_handler();
        usleep(5000);
    }
}